package com.gs.view.riepilogo;

import java.sql.SQLException;
import java.util.ArrayList;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.gs.R;
import com.gs.business.cache.CacheEntrate;
import com.gs.domain.Entrate;
import com.gs.view.entrate.EntrateView;
import comi.gsi.view.MyBaseActivity;

public class ListaEntrate extends MyBaseActivity {

	@Override
	protected int getIdLayout() {
		return R.layout.listattivita;
	}

	@Override
	protected void myOnCreate(final Bundle savedInstanceState) {

		ArrayList<Entrate> entr = null;
		try {
			entr = CacheEntrate.getSingleton().getAllEntrateForTest();
		}
		catch (SQLException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		catch (InstantiationException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		catch (IllegalAccessException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		catch (ClassNotFoundException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		ArrayAdapter<Entrate> adapter = new ArrayAdapter<Entrate>(this, R.layout.row, entr.toArray(new Entrate[entr.size()])) {
			@Override
			public View getView(final int position, View convertView, final ViewGroup parent) {
				ViewHolder viewHolder = null;
				if (convertView == null) {
					final LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
					convertView = inflater.inflate(R.layout.row, null);
					viewHolder = new ViewHolder();
					viewHolder.listNomeTrs = (TextView) convertView.findViewById(R.id.listNomeTrs);
					viewHolder.listTipoTrs = (TextView) convertView.findViewById(R.id.listTipoTrs);
					viewHolder.listEuroTrs = (TextView) convertView.findViewById(R.id.listEuroTrs);
					viewHolder.listDataTrs = (TextView) convertView.findViewById(R.id.listDataTrs);
					convertView.setTag(viewHolder);
				} else {
					viewHolder = (ViewHolder) convertView.getTag();
				}
				final Entrate uscita = getItem(position);

				viewHolder.listNomeTrs.setText(uscita.getNomeEntita() != null ? uscita.getNomeEntita() : "NO");
				viewHolder.listTipoTrs.setText(uscita.getFisseoVar() != null ? uscita.getFisseoVar() : "NO");
				viewHolder.listEuroTrs.setText(uscita.getinEuro() != 0 ? Double.toString(uscita.getinEuro()) : "NO");
				viewHolder.listDataTrs.setText(uscita.getdata() != null ? uscita.getdata() : "NO");
				convertView.setOnClickListener(new View.OnClickListener() {

					@Override
					public void onClick(final View v) {

						final Intent i = new Intent(ListaEntrate.this, EntrateView.class);
						i.putExtra("idEntrata", uscita.getidEntrate());
						try {
							startActivity(i);
						} catch (final Exception e) {
							Log.e("boh", e.getMessage());
						}

					}
				});

				return convertView;
			}
		};

		ListView list = (ListView) findViewById(R.id.listView1);
		list.setAdapter(adapter);

	}

	private static class ViewHolder {
		public TextView listNomeTrs;
		public TextView listTipoTrs;
		public TextView listEuroTrs;
		public TextView listDataTrs;
	}
}
